<template>
    <div class="UpPass">
        <el-tabs type="card">
            <el-tab-pane>
                <span slot="label"><i class="el-icon-key"></i> 修改密码</span>
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="原密码" prop="oldPass">
                        <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="pass">
                        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm()">提交</el-button>
                        <el-button @click="resetForm('ruleForm')">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-tab-pane>
            <el-tab-pane>
                <span slot="label"><i class="el-icon-unlock"></i> 找回密码</span>
                找回密码
            </el-tab-pane>
        </el-tabs>
    </div>
</template>
<script>
    import UpPassService from "../../model/UpPassService";
    const upPassService=UpPassService.getInstance()
    export default {
        name: "UpPass",
        data() {
            const checkOldPass = (rule, value, callback) => {
                if (value==='') {
                    return callback(new Error('原密码不能为空'));
                }
                setTimeout(() => {
                    callback();
                }, 1);
            };
            const validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (!/^[a-zA-Z0-9_]{3,10}$/.test(value)) {
                        callback(new Error('密码为3-10位(数字、字母、下划线)!'));
                    } else {
                        if (value === this.ruleForm.oldPass) {
                            callback(new Error('新密码不能与旧密码一致!'));
                        } else {
                            if (this.ruleForm.checkPass !== '') {
                                this.$refs.ruleForm.validateField('checkPass');
                            }
                            callback();
                        }
                    }
                }
            };
            const validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else {
                    if (value !== this.ruleForm.pass) {
                        callback(new Error('两次输入密码不一致!'));
                    }
                    callback();
                }
            };
            return {
                ruleForm: {
                    oldPass: '',
                    pass: '',
                    checkPass: ''
                },
                rules: {
                    oldPass: [
                        { validator: checkOldPass, trigger: 'blur' }
                    ],
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        const id = JSON.parse(localStorage["user"])[0].id
                        upPassService.queryByIdAndPass(id,this.ruleForm.oldPass,this.$http).then(msg=>{
                            if(msg.data.data){
                                const params=new URLSearchParams()
                                params.append("id",id)
                                params.append("userPwd",this.ruleForm.pass)
                                upPassService.upUserPwd(params,this.$http).then(msg=>{
                                    if(msg.data.status==="20000"){
                                        this.$message({
                                            type:"success",
                                            message:"修改成功!请牢记你的密码~"
                                        })
                                    }else{
                                        this.$message({
                                            type:"error",
                                            message:"出错了呢~"
                                        })
                                    }
                                });
                            }else{
                                this.$message({
                                    type:"error",
                                    message:"密码错误!"
                                })
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>
<style scoped>
    .el-form-item{
        margin-top: 50px;
        margin-left: 380px;
        width: 600px;
    }
</style>